/*
 * Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & dreamlu.net).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pj.project.mqtt.conf;


import com.pj.project.update_package.UpdatePackageService;
import net.dreamlu.iot.mqtt.codec.ByteBufferUtil;
import net.dreamlu.iot.mqtt.codec.MqttQoS;
import net.dreamlu.iot.mqtt.spring.client.MqttClientSubscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.ByteBuffer;


/**
 * 客户端连接状态监听
 *
 * @author L.cm
 */
@Service
public class MqttClientSubscribeListener {
    private static final Logger logger = LoggerFactory.getLogger(MqttClientSubscribeListener.class);

    @Autowired
    UpdatePackageService updatePackageService;

//    @MqttClientSubscribe("/test/#")
//    public void subQos0(String topic, ByteBuffer payload) {
//        logger.info("topic:{} payload:{}", topic, ByteBufferUtil.toString(payload));
//    }
//
//    @MqttClientSubscribe(value = "/qos1/#", qos = MqttQoS.AT_LEAST_ONCE)
//    public void subQos1(String topic, ByteBuffer payload) {
//        logger.info("topic:{} payload:{}", topic, ByteBufferUtil.toString(payload));
//    }

    @MqttClientSubscribe(value = "device/update/rx/+", qos = MqttQoS.AT_MOST_ONCE)
    public void update(String topic, ByteBuffer payload) {
        String replace = topic.replace("/rx/", "/tx/");
        updatePackageService.updatePackageCtl(replace, payload.array());
        logger.info("topic:{} payload:{}", topic, ByteBufferUtil.toString(payload));
    }

    @MqttClientSubscribe(value = "device/agct_lora/rx/+", qos = MqttQoS.AT_MOST_ONCE)
    public void agctLora(String topic, ByteBuffer payload) {
        logger.info("topic:{} payload:{}", topic, ByteBufferUtil.toString(payload));
    }


}
